R语言绘图中图片的组合(cowplot、patchwork宏包、layout、par()、gridExtra)

您所在的位置:网站首页 图形组合画的图片大全 小学生 R语言绘图中图片的组合(cowplot、patchwork宏包、layout、par()、gridExtra)

R语言绘图中图片的组合(cowplot、patchwork宏包、layout、par()、gridExtra)

2024-06-25 01:10| 来源: 网络整理| 查看: 265

组合图片 (1) cowplot

可以使用 cowplot 宏包的plot_grid()函数完成多张图片的组合,使用方法很简单。

p1 % ggplot(aes(x = gdpPercap, y = lifeExp)) + geom_point(aes(color = lifeExp > mean(lifeExp))) + scale_x_log10() + theme(legend.position = "none") + scale_color_manual(values = c("orange", "pink")) + labs( title = "My Plot Title", x = "The X Variable", y = "The Y Variable" ) p2 % ggplot(aes(x = gdpPercap, y = lifeExp, color = continent)) + geom_point() + scale_x_log10() + scale_color_manual( values = c("#195744", "#008148", "#C6C013", "#EF8A17", "#EF2917") ) + theme(legend.position = "none") + labs( title = "My Plot Title", x = "The X Variable", y = "The Y Variable" ) cowplot::plot_grid( p1, p2, labels = c("A", "B") )

img

参数及用法:http://www.idata8.com/rpackage/cowplot/plot_grid.html

示例:https://www.jianshu.com/p/6400fd3abc56

(2) patchwork宏包

也可以使用patchwork宏包,更简单的方法

library(patchwork) p1 + p2

img

p1 / p2

img

p1 + p2 + plot_annotation( tag_levels = "A", title = "The surprising truth about mtcars", subtitle = "These 3 plots will reveal yet-untold secrets about our beloved data-set", caption = "Disclaimer: None of these plots are insightful" )

img

library(palmerpenguins) g1 % ggplot(aes(bill_length_mm, body_mass_g, color = species)) + geom_point() + theme_bw(base_size = 14) + labs(tag = "(A)", x = "Bill length (mm)", y = "Body mass (g)", color = "Species") g2 % ggplot(aes(bill_length_mm, bill_depth_mm, color = species)) + geom_point() + theme_bw(base_size = 14) + labs(tag = "(B)", x = "Bill length (mm)", y = "Bill depth (mm)", color = "Species") g1 + g2 + patchwork::plot_layout(guides = "collect")

img

参考:https://bookdown.org/wangminjie/R4DS/baseR-intro-ds.html

(3) 自定义布局layout

用法

layout(mat, widths = rep.int(1, ncol(mat)), heights = rep.int(1, nrow(mat)), respect = FALSE) mat 参数为一个矩阵,提供了作图的顺序以及图形版面的安排。0代表空缺,不绘制图形,大于0 的数代表绘图顺序,相同数字代表占位符。widths 和 heights 参数提供了各个矩形作图区域的长和宽的比例。respect 参数控制着各图形内的横纵轴刻度长度的比例尺是否一样。n 参数为欲显示的区域的序号。 matrix(c(0,2,0,0,1,3),2,3,byrow = T) [,1] [,2] [,3] [1,] 0 2 0 [2,] 0 1 3 nf


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3